package cn.tedu.test.luban.rpc.server;

import cn.tedu.test.luban.rpc.HelloService;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    //创建socketserver服务端监听 20000
    public static void main(String[] args) throws IOException {
        ServerSocket server=new ServerSocket(20000);
        //保持服务端持续监听
        while(true){
            //等待有客户端发送socket
            System.out.println("wait for connecting......");
            Socket socket = server.accept();
            //从socket解析数据
            process(socket);
            System.out.println("服务端接收到客户端通信socket");
        }
    }

    private static void process(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        //包装一个高级流
        DataInputStream dis=new DataInputStream(inputStream);
        String name = dis.readUTF();
        //System.out.println("socket处理反序列化:"+name);
        HelloService helloService=new HelloServiceImpl();
        String result = helloService.sayHi(name);
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dos=new DataOutputStream(outputStream);
        dos.writeUTF(result);
    }
}
